package com.leetroll.media.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leetroll.api.service.ItemService;
import com.leetroll.common.entity.Item;
import com.leetroll.media.service.CleanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * item清理job
 * # 删除现有item
 * # 删除阿里云资源
 */
@Service
@Slf4j
public class Job_Clean {

    private final ItemService itemService;
    private final CleanService cleanService;

    @Autowired
    public Job_Clean(ItemService itemService, CleanService cleanService) {
        this.itemService = itemService;
        this.cleanService = cleanService;
    }

    public void deleteItem() {
        List<Item> waitingDelItems = getWaitingDelItems();
        cleanService.clean(waitingDelItems);
        log.info("finished");
    }

    private List<Item> getWaitingDelItems() {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cat_id", 1);
        queryWrapper.eq("source", "Gumroad");
        return itemService.list(queryWrapper);
    }
}
